﻿namespace Microsoft.SharePoint.Client
{
    using Microsoft.SharePoint.Client.Workflow;
    using System;
    using System.ComponentModel;

    [ScriptType("SP.ContentType", ServerTypeId="{91b5bd2d-e133-486f-b727-197ce5eb2c0d}")]
    public class ContentType : ClientObject
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public ContentType(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public void DeleteObject()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "DeleteObject", null);
            base.Context.AddQuery(query);
            base.RemoveFromParentCollection();
        }

        internal void InitFromCreationInformation(ContentTypeCreationInformation creation)
        {
            if (creation != null)
            {
                base.ObjectData.Properties["Description"] = creation.Description;
                base.ObjectData.Properties["Group"] = creation.Group;
                base.ObjectData.Properties["Name"] = creation.Name;
            }
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag)
            {
                switch (peekedName)
                {
                    case "DisplayFormTemplateName":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["DisplayFormTemplateName"] = reader.ReadString();
                        return flag;

                    case "EditFormTemplateName":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["EditFormTemplateName"] = reader.ReadString();
                        return flag;

                    case "NewFormTemplateName":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["NewFormTemplateName"] = reader.ReadString();
                        return flag;

                    case "NewFormUrl":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["NewFormUrl"] = reader.ReadString();
                        return flag;

                    case "EditFormUrl":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["EditFormUrl"] = reader.ReadString();
                        return flag;

                    case "DisplayFormUrl":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["DisplayFormUrl"] = reader.ReadString();
                        return flag;

                    case "Id":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Id"] = reader.Read<ContentTypeId>();
                        return flag;

                    case "ReadOnly":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["ReadOnly"] = reader.ReadBoolean();
                        return flag;

                    case "Name":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Name"] = reader.ReadString();
                        return flag;

                    case "Description":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Description"] = reader.ReadString();
                        return flag;

                    case "Hidden":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Hidden"] = reader.ReadBoolean();
                        return flag;

                    case "Scope":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Scope"] = reader.ReadString();
                        return flag;

                    case "Group":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["Group"] = reader.ReadString();
                        return flag;

                    case "FieldLinks":
                        flag = true;
                        reader.ReadName();
                        this.FieldLinks.FromJson(reader);
                        return flag;

                    case "WorkflowAssociations":
                        flag = true;
                        reader.ReadName();
                        this.WorkflowAssociations.FromJson(reader);
                        return flag;

                    case "DocumentTemplateUrl":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["DocumentTemplateUrl"] = reader.ReadString();
                        return flag;

                    case "DocumentTemplate":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["DocumentTemplate"] = reader.ReadString();
                        return flag;

                    case "SchemaXml":
                        flag = true;
                        reader.ReadName();
                        base.ObjectData.Properties["SchemaXml"] = reader.ReadString();
                        return flag;

                    case "Parent":
                        flag = true;
                        reader.ReadName();
                        this.Parent.FromJson(reader);
                        return flag;

                    case "Fields":
                        flag = true;
                        reader.ReadName();
                        this.Fields.FromJson(reader);
                        return flag;
                }
            }
            return flag;
        }

        [Remote]
        public void Update(bool updateChildren)
        {
            ClientAction query = new ClientActionInvokeMethod(this, "Update", new object[] { updateChildren });
            base.Context.AddQuery(query);
        }

        [Remote]
        public string Description
        {
            get
            {
                base.CheckUninitializedProperty("Description");
                return (string) base.ObjectData.Properties["Description"];
            }
            set
            {
                if (base.Context.ValidateOnClient && (value == null))
                {
                    throw ClientUtility.CreateArgumentNullException("value");
                }
                base.ObjectData.Properties["Description"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Description", value));
                }
            }
        }

        [Remote]
        public string DisplayFormTemplateName
        {
            get
            {
                base.CheckUninitializedProperty("DisplayFormTemplateName");
                return (string) base.ObjectData.Properties["DisplayFormTemplateName"];
            }
            set
            {
                base.ObjectData.Properties["DisplayFormTemplateName"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "DisplayFormTemplateName", value));
                }
            }
        }

        [Remote]
        public string DisplayFormUrl
        {
            get
            {
                base.CheckUninitializedProperty("DisplayFormUrl");
                return (string) base.ObjectData.Properties["DisplayFormUrl"];
            }
            set
            {
                base.ObjectData.Properties["DisplayFormUrl"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "DisplayFormUrl", value));
                }
            }
        }

        [Remote]
        public string DocumentTemplate
        {
            get
            {
                base.CheckUninitializedProperty("DocumentTemplate");
                return (string) base.ObjectData.Properties["DocumentTemplate"];
            }
            set
            {
                if (base.Context.ValidateOnClient && (value == null))
                {
                    throw ClientUtility.CreateArgumentNullException("value");
                }
                base.ObjectData.Properties["DocumentTemplate"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "DocumentTemplate", value));
                }
            }
        }

        [Remote]
        public string DocumentTemplateUrl
        {
            get
            {
                base.CheckUninitializedProperty("DocumentTemplateUrl");
                return (string) base.ObjectData.Properties["DocumentTemplateUrl"];
            }
        }

        [Remote]
        public string EditFormTemplateName
        {
            get
            {
                base.CheckUninitializedProperty("EditFormTemplateName");
                return (string) base.ObjectData.Properties["EditFormTemplateName"];
            }
            set
            {
                base.ObjectData.Properties["EditFormTemplateName"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "EditFormTemplateName", value));
                }
            }
        }

        [Remote]
        public string EditFormUrl
        {
            get
            {
                base.CheckUninitializedProperty("EditFormUrl");
                return (string) base.ObjectData.Properties["EditFormUrl"];
            }
            set
            {
                base.ObjectData.Properties["EditFormUrl"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "EditFormUrl", value));
                }
            }
        }

        [Remote]
        public FieldLinkCollection FieldLinks
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("FieldLinks", out obj2))
                {
                    return (FieldLinkCollection) obj2;
                }
                FieldLinkCollection links = new FieldLinkCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "FieldLinks"));
                base.ObjectData.ClientObjectProperties["FieldLinks"] = links;
                return links;
            }
        }

        [Remote]
        public FieldCollection Fields
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("Fields", out obj2))
                {
                    return (FieldCollection) obj2;
                }
                FieldCollection fields = new FieldCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "Fields"));
                base.ObjectData.ClientObjectProperties["Fields"] = fields;
                return fields;
            }
        }

        [Remote]
        public string Group
        {
            get
            {
                base.CheckUninitializedProperty("Group");
                return (string) base.ObjectData.Properties["Group"];
            }
            set
            {
                if (base.Context.ValidateOnClient)
                {
                    if ((value != null) && (value.Length == 0))
                    {
                        throw ClientUtility.CreateArgumentException("value");
                    }
                    if ((value != null) && (value.Length > 0x80))
                    {
                        throw ClientUtility.CreateArgumentException("value");
                    }
                }
                base.ObjectData.Properties["Group"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Group", value));
                }
            }
        }

        [Remote]
        public bool Hidden
        {
            get
            {
                base.CheckUninitializedProperty("Hidden");
                return (bool) base.ObjectData.Properties["Hidden"];
            }
            set
            {
                base.ObjectData.Properties["Hidden"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Hidden", value));
                }
            }
        }

        [Remote]
        public ContentTypeId Id
        {
            get
            {
                base.CheckUninitializedProperty("Id");
                return (ContentTypeId) base.ObjectData.Properties["Id"];
            }
        }

        [Remote]
        public string Name
        {
            get
            {
                base.CheckUninitializedProperty("Name");
                return (string) base.ObjectData.Properties["Name"];
            }
            set
            {
                if (base.Context.ValidateOnClient && (value == null))
                {
                    throw ClientUtility.CreateArgumentNullException("value");
                }
                base.ObjectData.Properties["Name"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "Name", value));
                }
            }
        }

        [Remote]
        public string NewFormTemplateName
        {
            get
            {
                base.CheckUninitializedProperty("NewFormTemplateName");
                return (string) base.ObjectData.Properties["NewFormTemplateName"];
            }
            set
            {
                base.ObjectData.Properties["NewFormTemplateName"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "NewFormTemplateName", value));
                }
            }
        }

        [Remote]
        public string NewFormUrl
        {
            get
            {
                base.CheckUninitializedProperty("NewFormUrl");
                return (string) base.ObjectData.Properties["NewFormUrl"];
            }
            set
            {
                base.ObjectData.Properties["NewFormUrl"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "NewFormUrl", value));
                }
            }
        }

        [Remote]
        public ContentType Parent
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("Parent", out obj2))
                {
                    return (ContentType) obj2;
                }
                ContentType type = new ContentType(base.Context, new ObjectPathProperty(base.Context, base.Path, "Parent"));
                base.ObjectData.ClientObjectProperties["Parent"] = type;
                return type;
            }
        }

        [Remote]
        public bool ReadOnly
        {
            get
            {
                base.CheckUninitializedProperty("ReadOnly");
                return (bool) base.ObjectData.Properties["ReadOnly"];
            }
            set
            {
                base.ObjectData.Properties["ReadOnly"] = value;
                if (base.Context != null)
                {
                    base.Context.AddQuery(new ClientActionSetProperty(this, "ReadOnly", value));
                }
            }
        }

        [Remote]
        public string SchemaXml
        {
            get
            {
                base.CheckUninitializedProperty("SchemaXml");
                return (string) base.ObjectData.Properties["SchemaXml"];
            }
        }

        [Remote]
        public string Scope
        {
            get
            {
                base.CheckUninitializedProperty("Scope");
                return (string) base.ObjectData.Properties["Scope"];
            }
        }

        [Remote]
        public WorkflowAssociationCollection WorkflowAssociations
        {
            get
            {
                object obj2;
                if (base.ObjectData.ClientObjectProperties.TryGetValue("WorkflowAssociations", out obj2))
                {
                    return (WorkflowAssociationCollection) obj2;
                }
                WorkflowAssociationCollection associations = new WorkflowAssociationCollection(base.Context, new ObjectPathProperty(base.Context, base.Path, "WorkflowAssociations"));
                base.ObjectData.ClientObjectProperties["WorkflowAssociations"] = associations;
                return associations;
            }
        }
    }
}

